//
//  TestSDViewController.m
//  OverseasOnlineShopping
//
//  Created by 唐先才 on 16/3/10.
//  Copyright © 2016年 唐先才. All rights reserved.
//

#import "TestSDViewController.h"
#import "AFNetworking.h"
#import "OverseasOnlineShopping-Swift.h"
#import "TxcModel.h"
#import "TxcPrefixHeader.pch"
#import "MJRefresh.h"
#import "MJRefreshHeader.h"
#import "MJRefreshFooter.h"

@interface TestSDViewController ()
{
    int _page;
    BOOL _isPulling;
    WaitLoadView *_waitView;
}
@end

@implementation TestSDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKCOLOR;
    _page = 1;
    [self createData];
    [self createRefresh];
    WaitLoadView *waitView = [[WaitLoadView alloc] initWithFrame:self.view.frame];
    _waitView = waitView;
    [self.view addSubview:waitView];
}

- (void)createData {
    if (_isPulling) {
        [self.baseDataArray removeAllObjects];
    }
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *page = [NSString stringWithFormat:@"%d", _page];
    NSDictionary *parametersDict = @{@"brightUrl":@"",
                                    @"catUrl":self.catUrl,
                                    @"exceptMallUrl":@"",
                                    @"homeShow":@"0",
                                    @"infoNation":@"0",
                                    @"infoType":@"100",
                                    @"isTop":@"1",
                                    @"keyWord":@"",
                                    @"mallUrl":@"",
                                    @"pageIndex": page,
                                    @"pageSize":@"20",
                                    @"tagUrl":@"",
                                    };
    
    [manager POST:SEARCHDET_URL parameters:parametersDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [_waitView removeFromSuperview];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dict in array) {
            TxcModel *model = [TxcModel modelWithDict:dict];
            [self.baseDataArray addObject:model];
        }
        NSLog(@"%@", self.baseDataArray);
        [self.baseCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)createRefresh {
    self.baseCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    self.baseCollectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
}

- (void)downRefresh {
    _isPulling = YES;
    _page = 1;
    [self.baseCollectionView.mj_header beginRefreshing];
    [self createData];
    [self.baseCollectionView.mj_header endRefreshing];
}

- (void)upRefresh {
    _isPulling = NO;
    _page ++;
    [self.baseCollectionView.mj_footer beginRefreshing];
    [self createData];
    [self.baseCollectionView.mj_footer endRefreshing];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
